package org.csu.jpetstorespringversion.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.websocket.Session;
import org.csu.jpetstorespringversion.mapper.AccountMapper;
import org.csu.jpetstorespringversion.mapper.ProfileMapper;
import org.csu.jpetstorespringversion.mapper.SignOnMapper;
import org.csu.jpetstorespringversion.model.Account;
import org.csu.jpetstorespringversion.model.Profile;
import org.csu.jpetstorespringversion.model.SignOn;
import org.csu.jpetstorespringversion.service.SignOnService;
import org.csu.jpetstorespringversion.service.serviceImpl.SignOnServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class SignOnController {

    @Autowired
    public SignOn signOn;
    @Autowired
    public Account account;

    @Autowired
    public Profile profile;

    @Autowired
    public ProfileMapper profileMapper;
    @Autowired
    public AccountMapper accountMapper;
    @Autowired
    public SignOnMapper signOnMapper;
    @Autowired
    public SignOnService signOnService;

    @Autowired
    public HttpServletRequest httpServletRequest;

    @Autowired
    public HttpSession session;

    @GetMapping("/loginForm")
    public String index(){
        return "loginForm";
    }

    @PostMapping("/SignOn")
    public String GetByUsernameAndPassword(@RequestParam("username") String username, @RequestParam("password") String password, Model model){

        //@RequestParam("code") Integer code
        //用户验证码的校验
        LambdaQueryWrapper<SignOn> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SignOn::getUsername,username).eq(SignOn::getPassword,password);
        SignOn signOn1 = signOnService.getOne(queryWrapper);
        if (signOn1 != null){
            System.out.println("success!!");
            session.setAttribute("signon",signOn1);
            model.addAttribute("signon",signOn1);
            account = accountMapper.selectById(username);
            profile = profileMapper.selectById(username);
            session.setAttribute("Account",account);
            session.setAttribute("Profile",profile);
            //配置account的session信息
            return "Account";
        }else{
            model.addAttribute("msg","输入的用户名或者密码错误，请重新输入！！");
            return "loginForm";
        }
    }

    @PostMapping("/accountOff")
    public String SignOff(){
        session.removeAttribute("Account");
        session.removeAttribute("signon");
        session.removeAttribute("Profile");
        return "loginForm";
    }

    @ResponseBody
    @GetMapping("/SignOn")
    public String GetByUsername (@RequestParam("username") String username, Model model){
        SignOn sign = signOnService.getById(username);
         if(sign != null){
            model.addAttribute("msg","该用户名已存在，请重新输入");
            return "Exist";
        }else{
            model.addAttribute("msg","当前用户名可用");
            return "No Exist";
        }
    }
}
